<?php

/**
*  新闻管理类
*/

class Articles{
	
	function Articles($db){
		$this->oDB = $db;
	}
	
	/**
	*  获取分类列表
	*/
	function getListCate($where='',$num=20){
		$sql = "SELECT * FROM articles_cate ORDER BY id ASC";
		return $this->oDB->query($sql,$num);
	}
	
	/**
	*  获取分类详细信息
	*/
	function getInfoCate($id){
		return $this->oDB->rows("SELECT * FROM articles_cate WHERE id='$id'");
	}
	
	/**
	*  更新分类资料
	*	data 	数组资料更新
	*	where 	更新条件不需要加where
	*/
	function updateCate($data,$where){
		return $this->oDB->update("articles_cate",$data,$where);
	}
	
	
	/**
	*  获取分类数量
	*/
	function getCateNum($id=0){
		if ($id>0){
			$num = $this->oDB->rows("SELECT COUNT(*) AS cnt FROM articles WHERE cate_id='$id'");
			return (int)$num['cnt'];
		}
	}
	
	/**
	*  插入分类资料
	*	data 	数组资料更新
	*	check	重复数据效验数组
	*/
	function insertCate($data,$check=''){
		
		//重复效验
		if (is_array($check)){
			unset($k,$v);
			foreach ($check AS $k=>$v) {
				$w[] = "`$k`='$v'";
			}
			$ck = $this->oDB->rows("SELECT count(*) AS cnt FROM articles_cate WHERE ".implode(' AND ',$w));
			
			//如果存在则提示
			if ($ck[cnt]>=1){
				return false;
			}
		}
		
		return $this->oDB->insert("articles_cate",$data);
	}
	
	/**
	*  删除分类资料
	*	id 		删除数据id
	*/
	function delCate($id){
		//删除文章
		$this->oDB->query("DELETE FROM articles WHERE cate_id='$id'");
		
		return $this->oDB->query("DELETE FROM articles_cate WHERE id='$id'");
	}
	
	
	
	/**
	*  获取列表
	*/
	function getList($where='',$num=20){
		$sql = "SELECT a.*,c.title FROM articles a LEFT JOIN articles_cate c ON c.id=a.cate_id $where ORDER BY a.id DESC";
		return $this->oDB->limit($sql, $_GET['page'], $num);
	}
	
	/**
	*  获取详细信息
	*/
	function getInfo($id){
		return $this->oDB->rows("SELECT * FROM articles WHERE id='$id'");
	}
	
	
	/**
	*  更新资料
	*	data 	数组资料更新
	*	where 	更新条件不需要加where
	*/
	function update($data,$where){
		return $this->oDB->update("articles",$data,$where);
	}
	
	
	/**
	*  插入资料
	*	data 	数组资料更新
	*	check	重复数据效验数组
	*/
	function insert($data,$check=''){
		
		//重复效验
		if (is_array($check)){
			unset($k,$v);
			foreach ($check AS $k=>$v) {
				$w[] = "`$k`='$v'";
			}
			$ck = $this->oDB->rows("SELECT count(*) AS cnt FROM articles WHERE ".implode(' AND ',$w));
			
			//如果存在则提示
			if ($ck[cnt]>=1){
				return false;
			}
		}
		
		return $this->oDB->insert("articles",$data);
	}
	
	/**
	*  删除资料
	*	id 		删除数据id
	*	filedir 删除文件文件夹
	*/
	function del($id,$filedir,$type=''){
		return $this->oDB->query("DELETE FROM articles WHERE id='$id'");
	}
	
	
	/**
	*  写入静态文件
	*	id		数据id
	*	tplName	静态模板名称
	*	写入静态目录
	*/
	function writeHtm($id,$tplName,$writeDir){
		global $tpl,$siteurl,$tpl_set,$oHtml;
		//获得资料
		$info = $this->getInfo($id);
		
		//获得关联分页号码
		$query = $this->oDB->query("SELECT * FROM articles WHERE subject='$info[subject]'");
		while($fp = $this->oDB->fetch_array($query)){
			$fp_ls[$fp['order']] = $fp['id'];
		}
		//检测数据多少
		if (count($fp_ls)>1){
			$tpl->set_var(array(
					'fp_ls'=>$fp_ls,
				)
			);
		}
		
		
		
		
		if ($info[id]==$id){
			
			//随机文章
			$about_query = $this->oDB->query("SELECT * FROM articles GROUP BY id ORDER BY rand()",10);
			while($fp = $this->oDB->fetch_array($about_query)){
				$about_list[] = $fp;
			}
			
			//随机新闻
			$news_query = $this->oDB->query("SELECT * FROM news GROUP BY id ORDER BY rand()",10);
			while($nl = $this->oDB->fetch_array($news_query)){
				$news_list[] = $nl;
			}
			
			$tpl->set_var(array(
					'about_list'=>$about_list,
					'news_list'=>$news_list,
					'interfix'=>unserialize($info['interfix']),
					'_info'=>$info,
					'_siteurl'=>$siteurl,
				)
			);
			$tpl->set_file($writeDir."_view",$tpl_set['TemplateDir'].'/'.$writeDir);
			$htm_data = $tpl->r();
			
			
			//写入数据提供资料
			$wdata 	= array(
				'write_id'=>$id,							//写入id
				'write_file'=>$writeDir,					//写入地址
				'subject'=>$info['subject'],				//文章标题
				'data'=>$htm_data,							//区块数据
			);
			
			//写入静态
			$oHtml->writer($wdata);
		}
	}
	
}



?>